package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    @ApiOperation("分页查询套餐")
    public Result<PageResult> queryPage(SetmealPageQueryDTO dto) {
        PageResult pageResult = setmealService.queryPage(dto);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @Cacheable(cacheNames = "setmeal",key = "#dto.getCategoryId()")
    public Result save(@RequestBody SetmealDTO dto) {
        setmealService.save(dto);
        return Result.success();
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result queryById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.queryById(id);
        return Result.success(setmealVO);
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)//万一改了分类怎么办，业务就复杂了，直接全部清理掉
    public Result update(@RequestBody SetmealDTO dto) {
        setmealService.updatebatech(dto);
        return Result.success();
    }
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @PostMapping("/status/{status}")
    @ApiOperation("停售起售套餐")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        setmealService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 批量删除套餐
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @DeleteMapping
    @ApiOperation("批量删除套餐")//批量删除都需要强制绑定，所以要加注解
    public Result deleteByIds(@RequestParam List<Long> ids) {
        setmealService.deleteByIds(ids);
        return Result.success();
    }
}

